<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 移除链表元素 -->
    <script>
      var removeElements = function (head, val) {
        // 如果头节点也需要删除，先找到链表中第一个不等于val的节点作为新的头节点
        while (head !== null && head.val === val) {
          head = head.next
        }
        let cur = head
        // 遍历链表，将val等于指定值的节点删除
        while (cur !== null && cur.next !== null) {
          if (cur.next.val === val) {
            cur.next = cur.next.next
          } else {
            cur = cur.next
          }
        }
        return head
      }
      console.log(removeElements([1, 2, 6, 3, 4, 5, 6], 6))
    </script>
  </body>
</html>
